<?php
/**
 *  +----------------------------------------------------------------------
 *  | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 *  +----------------------------------------------------------------------
 *  | Copyright (c) 2016~2025 https://www.crmeb.com All rights reserved.
 *  +----------------------------------------------------------------------
 *  | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 *  +----------------------------------------------------------------------
 *  | Author: CRMEB Team <admin@crmeb.com>
 *  +----------------------------------------------------------------------
 */

/**
 * 队伍申请
 * @author crud自动生成代码
 * @date 2025/01/15 00:00:00
 */

namespace app\dao\crud;

use app\dao\BaseDao;
use app\model\crud\TeamApplicationModel;

/**
 * Class TeamApplicationDao
 * @date 2025/01/15
 * @package app\dao\crud
 */
class TeamApplicationDao extends BaseDao
{

    /**
     * 设置模型
     * @return string
     * @date 2025/01/15
     */
    protected function setModel(): string
    {
        return TeamApplicationModel::class;
    }

    /**
     * 搜索
     * @param array $where
     * @return \crmeb\basic\BaseModel
     * @throws \ReflectionException
     * @date 2025/01/15
     */
    public function searchCrudModel(array $where = [], $field = ['*'], string $order = '', array $with = [])
    {
        return $this->getModel()->field($field)->when($order !== '', function ($query) use ($order) {
            $query->order($order);
        })->when($with, function ($query) use ($with) {
            $query->with($with);
        })->when(!empty($where['team_id']), function($query) use ($where) {
            $query->where('team_id', '=', $where['team_id']);
        })->when(!empty($where['uid']), function($query) use ($where) {
            $query->where('uid', '=', $where['uid']);
        })->when(isset($where['status']), function($query) use ($where) {
            $query->where('status', '=', $where['status']);
        })->when(!empty($where['create_time_start']), function($query) use ($where) {
            $query->where('create_time', '>=', $where['create_time_start']);
        })->when(!empty($where['create_time_end']), function($query) use ($where) {
            $query->where('create_time', '<=', $where['create_time_end']);
        });
    }

    /**
     * 获取用户的申请记录
     * @param int $uid
     * @param array $where
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserApplications(int $uid, array $where = [])
    {
        $where['uid'] = $uid;
        return $this->searchCrudModel($where, ['*'], 'create_time desc', ['team', 'user', 'baoming'])->select();
    }

    /**
     * 获取队伍的申请记录
     * @param int $teamId
     * @param array $where
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getTeamApplications(int $teamId, array $where = [])
    {
        $where['team_id'] = $teamId;
        return $this->searchCrudModel($where, ['*'], 'create_time desc')->select();
    }

    /**
     * 检查用户是否已申请该队伍
     * @param int $uid
     * @param int $teamId
     * @return bool
     */
    public function hasApplied(int $uid, int $teamId)
    {
        return $this->be(['uid' => $uid, 'team_id' => $teamId]);
    }

    /**
     * 获取待审核的申请数量
     * @param int $teamId
     * @return int
     */
    public function getPendingCount(int $teamId)
    {
        return $this->count(['team_id' => $teamId, 'status' => TeamApplicationModel::STATUS_PENDING]);
    }

    /**
     * 批量更新申请状态
     * @param array $ids
     * @param int $status
     * @return bool
     */
    public function batchUpdateStatus(array $ids, int $status)
    {
        return $this->batchUpdate($ids, ['status' => $status, 'update_time' => date('Y-m-d H:i:s')]);
    }

    public function getModel()
    {
        return $this->getModel();
    }
}
